/**
 * Created by zqk on 2018/4/23.
 */
$(document).ready(function () {
    var form = new xhForm($('#xh_form'));
    form.parse();
    if(!scheduleId){
        var sTime = new Date().format('yyyy-MM-dd 09:00:00');//前一个小时
        var eTime = new Date().format('yyyy-MM-dd 17:30:00');//前一个小时
        $("#tFromAt").datetimepicker({format: 'yyyy-mm-dd hh:ii:ss',startDate:'2010-01-01',language: 'zh-CN', autoclose:true}).val(sTime);
        $("#tEndAt").datetimepicker({format: 'yyyy-mm-dd hh:ii:ss',startDate:'2010-01-01',language: 'zh-CN', autoclose:true}).val(eTime);
    }else{
        $("#tFromAt").datetimepicker({format: 'yyyy-mm-dd hh:ii:ss',startDate:'2010-01-01',language: 'zh-CN', autoclose:true});
        $("#tEndAt").datetimepicker({format: 'yyyy-mm-dd hh:ii:ss',startDate:'2010-01-01',language: 'zh-CN', autoclose:true});
    }

    $('#bt_form_add').click(function () {
        var sTitle = $('#sScheduleName', window.self.document).val();
        if(!sTitle){
            window.top.xhTop.showMessage({title: '标题不能为空！', type: 'error'});
            return false;
        }
        var tStartTime = $('#tFromAt', window.self.document).val();
        if(!tStartTime){
            window.top.xhTop.showMessage({title: '开始时间不能为空！', type: 'error'});
            return false;
        }
        var tEndTime = $('#tEndAt', window.self.document).val();
        var start = Date.parse(tStartTime);
        var end = Date.parse(tEndTime);
        var now = Date.parse(new Date());
        if(end && start > end){
            window.top.xhTop.showMessage({title: '结束时间大于开始时间！', type: 'error'});
            return false;
        }
        var sid = $('#iScheduleId').val();
        var status = $("input[name='iScheduleStatus']:checked").val();
        if(!status){
            status = 1;
        }
        var events = {};
        events.title = sTitle;
        events.start = tStartTime;
        events.end = tEndTime;
        events.status = status;
        if (status==1 && end > now) {//未开始 未过期
            var color = '#CCCCCC';
        } else if(status == 2 && end >= now) {//进行中 未过期
            var color = '#6495ED';
        } else if(status == 3) {//已完成
            var color = '#66CDAA';
        } else if (status == 4) {//已关闭
            var color = '#EEEE00';
        } else if((status ==1||status==2) && end<now) {//过期未完成
            var color = '#EE3B3B';
        }
        events.color = color;
        $.ajax({
            url:moduleUrl+'schedule/addSchedule',
            data:$('#xh_form').serialize(),
            type:'POST',
            success:function (data) {
                data = JSON.parse(data);
                window.top.xhTop.showSuccess(
                        '操作成功',
                );
                window.top.xhTop.closeModal();
                if(scheduleId){
                    events.id = scheduleId;
                    window.top.xhTop.getActiveFrame().editSchedule(events);
                }else{
                    events.id = data['id'];
                    window.top.xhTop.getActiveFrame().addSchedule(events);
                }
            },
            error:function (data) {
                window.top.xhTop.showError(
                    data.responseJSON.error,
                    data.responseJSON.msg
                );
            }
        });

    });

    $('#bt_form_back').click(function() {
        window.top.xhTop.closeModal();
    });


    $('#bt_form_del').click(function () {
        $.ajax({
            url:moduleUrl+'schedule/delScheduleById',
            data:{iScheduleId:scheduleId},
            type:'POST',
            success:function (data) {
                // window.top.swal(data['responseText'],'','success');
                window.top.xhTop.showSuccess(
                    data.msg
                );
                window.top.xhTop.getActiveFrame().removeSchedule(scheduleId);
                window.top.xhTop.closeModal();
            },
            error:function (data) {
                window.top.xhTop.showError(
                    data.responseJSON.error,
                    data.responseJSON.msg
                );
            }
        });

    });

});